//builde the outer legend table
var compactFooter = false;
var rowsinlegend = 8;
var maxColumns = 4;
var maxCharacters = 14;

var LegendInfo;

function builedlagend(legendInfo, papercase) {
    // paper A4
    // calc the number of colomns in legend
    //var arri = legendjson.split("@");
    if (papercase == "3") {
        maxColumns = 7;
    }
    if (papercase == "2") {
        maxColumns = 13;
    }
    if (papercase == "1") {
        maxColumns = 20;
    }
    if (Orientation != "portrait") {
        maxColumns = maxColumns * 1.5;
    }
    
    LegendInfo = legendInfo;
    var rows = new Array();

    for (rowIndex = 0; rowIndex < LegendInfo.length; rowIndex++) {
        //rows.push(CreateLegendRow(LegendInfo[rowIndex], -1));
        if (LegendInfo[rowIndex].Children.length > 0) {
            for (childIndex = 0; childIndex < LegendInfo[rowIndex].Children.length; childIndex++) {
                rows.push(CreateLegendRow(LegendInfo[rowIndex], childIndex));
                //tdTable += CreateLegendRow(LegendInfo[rowIndex], childIndex);
            }
        }
        else {
            rows.push(CreateLegendRow(LegendInfo[rowIndex], -1));
        }
    }

    if (!isMinimap) {
        maxColumns += 2;
        maxCharacters += 2;
    }
    if (!isLogo) {
        maxColumns += 2;
        maxCharacters += 2;
    }
    var colcounter = Math.ceil(rows.length / rowsinlegend)
    if (colcounter > 2) compactFooter = true;
    var toc = "<table dir='rtl' cellpadding='0' cellspacing='0' style='height: 145px; border: solid 1px silver'><tr>"
    for (i = 0; i < colcounter && i < maxColumns; i++) {
        toc += "<td valign='top'>" + tocLegend(i * rowsinlegend, rowsinlegend, rows) + "</td>"
    }
    toc += "</tr></table>";

    return toc;
}

function tocLegend(inx, trows, rows) {
    var tdTable = "<table dir='rtl' cellpadding='2' cellspacing='0' style='font-size: 10px'>";
    //var arri = lgnjson.split("@")
    var ubound = inx + trows
    for (var i = inx; i < ubound; i++) {
        /*
        tdTable += CreateLegendRow(LegendInfo[i], -1);
        if(LegendInfo[i].Children.length > 0) {
        for(childIndex = 0; childIndex < LegendInfo[i].Children.length; childIndex++) {
        tdTable += CreateLegendRow(LegendInfo[i], childIndex);
        }
        }
        */
        tdTable += rows[i];
        if (i == rows.length-1)
            break;
    }
    tdTable += "</table>";

    return tdTable;

}

function CreateLegendRow(layerInfo, childIndex) {
  /*  debugger;/*/
    var imglegend = "<img src='" + CreateImageUrl(layerInfo, childIndex) + "' style='zoom: 0.7'>";
    var row = "<tr><td>" + imglegend + "</td><td nowrap " + (childIndex == -1 ? " style='font-weight:bold'" : "") + ">" + GetLabel(layerInfo, childIndex) + "</td></tr>";
    return row;
}
function CreateImageUrl(layerInfo, childIndex) {
  /*  debugger;*/
    var legendIndex = -1;
    var geomType = -1;
    if (layerInfo.Children.length > 0) {
        if (childIndex >= 0 && childIndex < layerInfo.Children.length) {
            legendIndex = layerInfo.Children[childIndex].LegendIndex;
            geomType = layerInfo.Children[childIndex].GeomType;
        }
    }
    return webAgent + "?OPERATION=GETLEGENDIMAGE&SESSION=" + sessionId + "&VERSION=1.0.0&SCALE=" + scale + "&LAYERDEFINITION=" + layerInfo.Path + "&THEMECATEGORY=" + legendIndex + "&TYPE=" + geomType;
}
function GetLabel(layerInfo, childIndex) {
    if (childIndex >= 0 && childIndex < layerInfo.Children.length) {
        legendLabel = layerInfo.Children[childIndex].Label;
    }
    else {
        legendLabel = layerInfo.Label;
    }
    if (legendLabel.length > maxCharacters) {
        var rx = new RegExp("^.{" + (maxCharacters - 5) + "}[^\\s]*");
        legendLabel = rx.exec(legendLabel.substr(0, maxCharacters));

        /*
        var splitted = legendLabel.split(" ");
        for (var i = 0; i < splitted.length; i++) {
        splitted[i]
        }
        var result = legendLabel.substr(0, maxCharacters);
        var lastIndex = result.lastIndexOf(" ");
        if (lastIndex >= 10) {
        result = result.substr(0, lastIndex);
        }
        */
    }
    return legendLabel;
    //return result;
}
// =================================================================================================================
function ValidateMapResponse(xmlRoot) {
   
    try {
        if (xmlRoot.tagName != "Envelope")
            return false;

        var xs = xmlRoot.getElementsByTagName("X");
        var ys = xmlRoot.getElementsByTagName("Y");
        parseFloat(xs[0].childNodes[0].nodeValue); parseFloat(ys[0].childNodes[0].nodeValue);
        parseFloat(xs[1].childNodes[0].nodeValue); parseFloat(ys[1].childNodes[0].nodeValue);

        var envp = document.getElementById("envp");

        //Calculate LAT (y) interval: left/right
        //Calculate LON (X) interval: top/bottom
        var x0 = parseInt(xs[0].childNodes[0].nodeValue); var x1 = parseInt(xs[1].childNodes[0].nodeValue);
        var y0 = parseInt(ys[0].childNodes[0].nodeValue); var y1 = parseInt(ys[1].childNodes[0].nodeValue);
        var x_ = parseInt((x1 - x0) / 4); var y_ = parseInt((y1 - y0) / 4);
        var cell;

        //Write Coord at the outer net
        for (i = 1; i <= 4; i++) {
            var yi = new String(y0 + y_ * i); var xi = new String(x0 + x_ * i);
            if (i == 1) {
                //Write Coord at the left-bottom outer net  
                var x0_ = new String(x0);
                var s0 = "<table style='z-index:1;font-size:9px' border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align='right'>" +
			 xi.substr(0, 3) + "<br/>" + xi.substr(3, 3) + "</td><td align='left'>" + x0_.substr(0, 3) + "<br/>" + x0_.substr(3, 3) + "</td></tr></table>"
                /*
                xi.substr(0,3)+"<image id='spacer' src='../stdicons/transparentpixel.gif' style='width:" + (imgpixelw == 230 ? imgpixelw - 20 : imgpixelw) + "; height:1px' align='absmiddle'>"+
                x0_.substr(0,3)+"<br>"+
                xi.substr(3,3)+"<image id='spacer' src='../stdicons/transparentpixel.gif' style='width:" + (imgpixelw == 230 ? imgpixelw - 20: imgpixelw) + "; height:1px' align='absmiddle'>"+
                x0_.substr(3,3)+"</td></tr></table>"
                */
                cell = document.getElementById('b' + i); cell.innerHTML = s0;
                cell = document.getElementById('t' + i); cell.innerHTML = s0;

                var y0_ = new String(y0);
                var s0 = "<table style='z-index:1;font-size:9px' border='0' cellpadding='0' cellspacing='0'><tr><td>" +
					  yi.substr(0, 3) + "<br>" + yi.substr(3, 3) + "<br>" +
					  "<image id='spacer' src='../stdicons/transparentpixel.gif' style='width:1px; height:" + imgpixelh + "px'><br>" +
					  y0_.substr(0, 3) + "<br>" + y0_.substr(3, 3) + "</td></tr></table>"
                cell = document.getElementById('l' + i); cell.innerHTML = s0;
                cell = document.getElementById('r' + i); cell.innerHTML = s0;
            } else {
                cell = document.getElementById('l' + i); cell.innerHTML = yi.substr(0, 3) + '<br>' + yi.substr(3, 3);
                cell = document.getElementById('r' + i); cell.innerHTML = yi.substr(0, 3) + '<br>' + yi.substr(3, 3);
                cell = document.getElementById('t' + i); cell.innerHTML = xi.substr(0, 3) + '<br>' + xi.substr(3, 3);
                cell = document.getElementById('b' + i); cell.innerHTML = xi.substr(0, 3) + '<br>' + xi.substr(3, 3);
            }
        }

        return true;
    } catch (e) { }
    return false;
}

// =================================================================================================================
function BuildeMap_PrintoutNET(nettype, rows, cols, tdcss) {

    var maptd_innerHTML = "<table dir='rtl'  id='map' border=0 cellpadding=0 cellspacing=0 style='border-top: 1px solid #000000;border-left: 1px solid #000000;border-right: 1px solid #000000;'>"
    switch (nettype) {
        case 'coord':

        case 'report':
            for (i = 0; i < rows - 1; i++) {
                maptd_innerHTML += "<tr>"
                for (j = 0; j < cols; j++) {
                    maptd_innerHTML += "<td class='" + tdcss + "'>.</td>"
                }
                maptd_innerHTML += "</tr>"
            }
            break;
        default:
            cols = 1;
            break;
    }

    maptd_innerHTML += "<tr>"
    for (j = 0; j < cols; j++)
        maptd_innerHTML += "<td class='net' style='border-bottom: 1px solid #000000;'>.</td>"

    return maptd_innerHTML + "</tr></table>";

}

function BuildeFrame_PrintoutNET(tblID, tdID, rows, cols, tdcss) {
    var i;
    var maptd_innerHTML = "<table dir='rtl'  id='" + tblID + "'   style='" + (rows == 1 ? "border-left: 1px solid #000000;" : "") + "' border=0 cellpadding=0 cellspacing=0>"

    for (i = rows; i > 1; i--) {
        maptd_innerHTML += "<tr>"
        for (j = 0; j < cols; j++)
            maptd_innerHTML += "<td id='" + tdID + i + "'  class='" + tdcss + "' align='center'>.</td>"
        maptd_innerHTML += "</tr>"
    }

    maptd_innerHTML += "<tr>"

    if (cols == 1) {
        for (j = 0; j < cols; j++)
            maptd_innerHTML += "<td id='" + tdID + i + "'  class='" + tdcss + "' style='border-bottom: 1px solid #000000;' align='center'>.</td>"
    }

    if (rows == 1) {
        for (j = cols; j > 0; j--)
            maptd_innerHTML += "<td id='" + tdID + j + "'  class='" + tdcss + "'>.</td>"

    }
    return maptd_innerHTML + "</tr></table>";
}

function Builed_OuterMapNet(nettype, papersize, orientation) {

    var rows = 0, cols = 0;
    rows = 4; cols = 4;

    var maptd = document.getElementById("Map-td"); maptd.innerHTML = BuildeMap_PrintoutNET(nettype, rows, cols, "net");
    maptd = document.getElementById("Map-right-td"); maptd.innerHTML = BuildeFrame_PrintoutNET("Map-right", "r", rows, 1, tdh);
    maptd = document.getElementById("Map-left-td"); maptd.innerHTML = BuildeFrame_PrintoutNET("Map-left", "l", rows, 1, tdh);
    maptd = document.getElementById("Map-top-td"); maptd.innerHTML = BuildeFrame_PrintoutNET("Map-top", "t", 1, cols, tdw);
    maptd = document.getElementById("Map-bottom-td"); maptd.innerHTML = BuildeFrame_PrintoutNET("Map-bottom", "b", 1, cols, tdw);

}

// =================================================================================================================
